<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="单位编码" prop="companyCode">
        <el-input
          v-model="queryParams.companyCode"
          placeholder="请输入单位编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单位名称" prop="companyName">
        <el-input
          v-model="queryParams.companyName"
          placeholder="请输入单位名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单位简称" prop="companyAbbr">
        <el-input
          v-model="queryParams.companyAbbr"
          placeholder="请输入单位简称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单位类型" prop="companyType">
        <el-select
          v-model="queryParams.companyType"
          placeholder="请选择单位类型"
          clearable
        >
          <el-option
            v-for="dict in dict.type.zhjj_unit_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="企业社会统一信用代码" prop="unifiedSocialCreditCode">
        <el-input
          v-model="queryParams.unifiedSocialCreditCode"
          placeholder="请输入企业社会统一信用代码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系人" prop="contactPerson">
        <el-input
          v-model="queryParams.contactPerson"
          placeholder="请输入联系人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系电话" prop="contactPhone">
        <el-input
          v-model="queryParams.contactPhone"
          placeholder="请输入联系电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单位状态" prop="unitState">
        <el-select
          v-model="queryParams.unitState"
          placeholder="请选择单位状态"
          clearable
        >
          <el-option
            v-for="dict in dict.type.zhjj_unit_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="资质等级" prop="qualificationLevel">
        <el-input
          v-model="queryParams.qualificationLevel"
          placeholder="请输入资质等级"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="注册资金" prop="registerCapital">
        <el-input
          v-model="queryParams.registerCapital"
          placeholder="请输入注册资金"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="注册地址" prop="registerAddress">
        <el-input
          v-model="queryParams.registerAddress"
          placeholder="请输入注册地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="主要业绩" prop="achievement">
        <el-input
          v-model="queryParams.achievement"
          placeholder="请输入主要业绩"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建人" prop="createBy">
        <el-input
          v-model="queryParams.createBy"
          placeholder="请输入创建人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['zhjj:contractor:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['zhjj:contractor:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['zhjj:contractor:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['zhjj:contractor:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="contractorList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键ID" align="center" prop="extSysId" />
      <el-table-column label="单位编码" align="center" prop="companyCode" />
      <el-table-column label="单位名称" align="center" prop="companyName" />
      <el-table-column label="单位简称" align="center" prop="companyAbbr" />
      <el-table-column label="单位类型" align="center" prop="companyType">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.zhjj_unit_type"
            :value="scope.row.companyType"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="企业社会统一信用代码"
        width="160"
        align="center"
        prop="unifiedSocialCreditCode"
      />
      <el-table-column label="联系人" align="center" prop="contactPerson" />
      <el-table-column label="联系电话" align="center" prop="contactPhone" />
      <el-table-column label="单位状态" align="center" prop="unitState">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.zhjj_unit_status"
            :value="scope.row.unitState"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="资质等级"
        align="center"
        prop="qualificationLevel"
      />
      <el-table-column label="注册资金" align="center" prop="registerCapital" />
      <el-table-column label="注册地址" align="center" prop="registerAddress" />
      <el-table-column label="主要业绩" align="center" prop="achievement" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="创建人" align="center" prop="createBy" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        fixed="right"
        width="120"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['zhjj:contractor:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['zhjj:contractor:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改承包商对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="80px"
        inline
      >
        <el-form-item label="单位编码" prop="companyCode">
          <el-input v-model="form.companyCode" placeholder="请输入单位编码" />
        </el-form-item>
        <el-form-item label="单位名称" prop="companyName">
          <el-input v-model="form.companyName" placeholder="请输入单位名称" />
        </el-form-item>
        <el-form-item label="单位简称" prop="companyAbbr">
          <el-input v-model="form.companyAbbr" placeholder="请输入单位简称" />
        </el-form-item>
        <el-form-item label="单位类型" prop="companyType">
          <el-select v-model="form.companyType" placeholder="请选择单位类型">
            <el-option
              v-for="dict in dict.type.zhjj_unit_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="企业社会统一信用代码"
          prop="unifiedSocialCreditCode"
        >
          <el-input
            v-model="form.unifiedSocialCreditCode"
            placeholder="请输入企业社会统一信用代码"
          />
        </el-form-item>
        <el-form-item label="联系人" prop="contactPerson">
          <el-input v-model="form.contactPerson" placeholder="请输入联系人" />
        </el-form-item>
        <el-form-item label="联系电话" prop="contactPhone">
          <el-input v-model="form.contactPhone" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="单位状态" prop="unitState">
          <el-select v-model="form.unitState" placeholder="请选择单位状态">
            <el-option
              v-for="dict in dict.type.zhjj_unit_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="资质等级" prop="qualificationLevel">
          <el-input
            v-model="form.qualificationLevel"
            placeholder="请输入资质等级"
          />
        </el-form-item>
        <el-form-item label="注册资金" prop="registerCapital">
          <el-input
            v-model="form.registerCapital"
            placeholder="请输入注册资金"
          />
        </el-form-item>
        <el-form-item label="注册地址" prop="registerAddress">
          <el-input
            v-model="form.registerAddress"
            placeholder="请输入注册地址"
          />
        </el-form-item>
        <el-form-item label="主要业绩" prop="achievement">
          <el-input v-model="form.achievement" placeholder="请输入主要业绩" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listContractor,
  getContractor,
  delContractor,
  addContractor,
  updateContractor,
} from "@/api/zhjj/contractor";

export default {
  name: "Contractor",
  dicts: ["zhjj_unit_type", "zhjj_unit_status"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 承包商表格数据
      contractorList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        companyCode: null,
        companyName: null,
        companyAbbr: null,
        companyType: null,
        unifiedSocialCreditCode: null,
        contactPerson: null,
        contactPhone: null,
        unitState: null,
        qualificationLevel: null,
        registerCapital: null,
        registerAddress: null,
        achievement: null,
        createBy: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        // 单位编码
        companyCode: [
          { required: true, message: "单位编码不能为空", trigger: "blur" },
          {
            min: 1,
            max: 50,
            message: "单位编码长度应在1到50个字符之间",
            trigger: "blur",
          },
        ],

        // 单位名称
        companyName: [
          { required: true, message: "单位名称不能为空", trigger: "blur" },
          {
            min: 1,
            max: 100,
            message: "单位名称长度应在1到100个字符之间",
            trigger: "blur",
          },
        ],

        // 单位简称
        companyAbbr: [
          { max: 50, message: "单位简称长度不能超过50个字符", trigger: "blur" },
        ],

        // 单位类型
        companyType: [
          { required: true, message: "单位类型不能为空", trigger: "change" },
          { type: "number", message: "单位类型必须是数字", trigger: "change" },
          {
            validator: (rule, value, callback) => {
              if (value < 1) {
                callback(new Error("单位类型必须大于等于1"));
              } else {
                callback();
              }
            },
            trigger: "change",
          },
        ],

        // 企业社会统一信用代码
        unifiedSocialCreditCode: [
          {
            pattern: /^[0-9A-Z]{18}$/,
            message: "企业社会统一信用代码必须是18位的数字和大写字母",
            trigger: "blur",
          },
        ],

        // 联系人
        contactPerson: [
          { required: true, message: "联系人不能为空", trigger: "blur" },
          {
            min: 1,
            max: 50,
            message: "联系人长度应在1到50个字符之间",
            trigger: "blur",
          },
        ],

        // 联系电话
        contactPhone: [
          { required: true, message: "联系电话不能为空", trigger: "blur" },
          {
            pattern: /^\\d{11}$/,
            message: "联系电话必须是11位数字",
            trigger: "blur",
          },
        ],

        // 单位状态
        unitState: [
          { required: true, message: "单位状态不能为空", trigger: "change" },
          { type: "number", message: "单位状态必须是数字", trigger: "change" },
          {
            validator: (rule, value, callback) => {
              if (value < 0) {
                callback(new Error("单位状态必须大于等于0"));
              } else {
                callback();
              }
            },
            trigger: "change",
          },
        ],

        // 资质等级
        qualificationLevel: [
          { max: 50, message: "资质等级长度不能超过50个字符", trigger: "blur" },
        ],

        // 注册资金（元）
        registerCapital: [
          { required: true, message: "注册资金不能为空", trigger: "blur" },
          {
            pattern: /^\d+(\.\d{1,2})?$/,
            message: "注册资金必须是数字，最多保留两位小数",
            trigger: "blur",
          },
        ],

        // 注册地址
        registerAddress: [
          { required: true, message: "注册地址不能为空", trigger: "blur" },
          {
            min: 1,
            max: 200,
            message: "注册地址长度应在1到200个字符之间",
            trigger: "blur",
          },
        ],

        // 主要业绩
        achievement: [
          {
            max: 500,
            message: "主要业绩长度不能超过500个字符",
            trigger: "blur",
          },
        ],

        // 创建人
        createBy: [
          { required: true, message: "创建人不能为空", trigger: "blur" },
          { max: 30, message: "单位简称长度不能超过50个字符", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询承包商列表 */
    getList() {
      this.loading = true;
      listContractor(this.queryParams).then((response) => {
        this.contractorList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        extSysId: null,
        companyCode: null,
        companyName: null,
        companyAbbr: null,
        companyType: null,
        unifiedSocialCreditCode: null,
        contactPerson: null,
        contactPhone: null,
        unitState: null,
        qualificationLevel: null,
        registerCapital: null,
        registerAddress: null,
        achievement: null,
        remark: null,
        createTime: null,
        updateTime: null,
        delFlag: null,
        createBy: null,
        updateBy: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.extSysId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加承包商";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const extSysId = row.extSysId || this.ids;
      getContractor(extSysId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改承包商";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.extSysId != null) {
            updateContractor(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addContractor(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const extSysIds = row.extSysId || this.ids;
      this.$modal
        .confirm('是否确认删除承包商编号为"' + extSysIds + '"的数据项？')
        .then(function () {
          return delContractor(extSysIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "zhjj/contractor/export",
        {
          ...this.queryParams,
        },
        `contractor_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
